package top.leoseven.adminproject.view;

import android.app.ProgressDialog;
import android.os.Build;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Scroller;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import top.leoseven.adminproject.R;
import top.leoseven.adminproject.modle.HomeWork;
import top.leoseven.adminproject.modle.KeCheng;
import top.leoseven.adminproject.modle.Student;
import top.leoseven.adminproject.presenter.MainPresenter;
import top.leoseven.adminproject.service.MainService;
import top.leoseven.adminproject.view.adapter.MainAdapter;
import top.leoseven.adminproject.view.adapter.MainPagerAdapter;

public class MainActivity extends BaseActivity implements MainService {
    private MainPresenter presenter;
    private TextView teachername;//教师名
    private TextView stucount;//学生人数
    private TextView kccount;//课程数
    private TextView homeworkcount;//作业数
    private TextView unReadHomeWorkNum;//未批阅消息数
    private TabLayout listtitle;
    private ViewPager page;
    private List<Student> stus;
    private List<KeCheng> kcs;
    private List<HomeWork> works;
    private List<Fragment> fragments;
    private MainAdapter stusadapter;
    private ProgressDialog dialog;
    private MainAdapter kcadapter;
    private MainAdapter workadapter;

    @Override
    public void setLayout() {
        setContentView(R.layout.activity_main);
        Window window = this.getWindow();
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
            return;
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(0xff40e0d0);
        teachername= (TextView) findViewById(R.id.username);
        stucount= (TextView) findViewById(R.id.stunum);
        kccount= (TextView) findViewById(R.id.kechengnum);
        homeworkcount= (TextView) findViewById(R.id.homework_num);
        page= (ViewPager) findViewById(R.id.page);
        listtitle= (TabLayout) findViewById(R.id.list_title);
        dialog=new ProgressDialog(this);
        dialog.setMessage("正在加载数据");
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog!=null) {
            dialog.cancel();
        }
    }

    @Override
    public void initPresenter() {
        works=new ArrayList<>();
        kcs=new ArrayList<>();
        stus=new ArrayList<>();
        fragments=new ArrayList<>();

        ListFragment fg1 = new ListFragment();
        stusadapter = new MainAdapter(stus, this);
        fg1.init(stusadapter,new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        fragments.add(fg1);

        ListFragment fg2 = new ListFragment();
        kcadapter = new MainAdapter(kcs,this);
        fg2.init(kcadapter,new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        fragments.add(fg2);

        ListFragment fg3=new ListFragment();
        workadapter = new MainAdapter(works,this);
        fg3.init(workadapter,new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        fragments.add(fg3);

        MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager(), fragments);
        page.setAdapter(adapter);
//        listtitle.addTab(listtitle.newTab());
//        listtitle.getTabAt(0).setText("学生");
//        listtitle.addTab(listtitle.newTab());
//        listtitle.getTabAt(1).setText("课程");
        listtitle.setupWithViewPager(page);
        presenter=new MainPresenter(this,this);
        presenter.initData();
    }

    @Override
    public void onDataLoadFinish(List<Student> students, List<HomeWork> works, List<KeCheng> keChengs) {
        stucount.setText(students.size()+"");
        homeworkcount.setText(""+works.size());
        kccount.setText(""+keChengs.size());
        stus.addAll(students);
        stusadapter.notifyDataSetChanged();
        kcs.addAll(keChengs);
        kcadapter.notifyDataSetChanged();
        this.works.addAll(works);
        workadapter.notifyDataSetChanged();
        dialog.dismiss();
    }

    @Override
    public void onError(String e) {
        dialog.dismiss();
    }
}
